import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_app_info.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/extension/extension_list.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/xindong_name_class.dart';
import 'package:xindong/models/xindong_tag_class.dart';
import 'package:xindong/models/xindong_task_class.dart';
import 'package:xindong/models/user/xindong_wode_user.dart';
import 'package:xindong/handle/xindong_user_handle.dart';

class XinDongWoDeHandle {
  final _userHandle = XinDongUserHandle();

  // 用户信息
  Future<XinDongWoDeUser?> userInfo() async {
    final data = await _userHandle.userInfo(userId: XinDongUserController.to.loginId);
    if (data != null) {
      return XinDongWoDeUser.fromJson(data);
    }
    return XinDongUserController.to.userInfo;
  }

  // 配置
  Future<bool> memberConfig() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userMemberConfig,
      method: XinDongHttpMethod.post,
    );
    return object.code == XinDongServerStatus.success;
  }

  // 配置
  Future<List<XinDongNameClass>> memberOccupation() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userMemberOccupation,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      return list.map((e) => XinDongNameClass.fromJson(e)).toList();
    }
    return [];
  }

  // 配置
  Future<List<XinDongTagClass>> userTagsList() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userTagsList,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      return list.map((e) => XinDongTagClass.fromJson(e)).toList();
    }
    return [];
  }

  // 任务(二维数组)
  Future<List<List<XinDongTaskClass>>> userTaskList() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userTaskList,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      return list.map((e) => (e as List).map((sub) => XinDongTaskClass.fromJson(sub)).toList()).toList();
    }
    return [];
  }

  // 配置
  Future<bool> userUpdateTags({required List<int> tags}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userUpdateTags,
      method: XinDongHttpMethod.post,
      parameters: {"tags": tags.listToString(pattern: ",")},
    );
    return object.code == XinDongServerStatus.success;
  }

  // 配置
  Future<bool> userCreateTag({required String tagName}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userCreateTag,
      method: XinDongHttpMethod.post,
      parameters: {"name": tagName},
    );
    return object.code == XinDongServerStatus.success;
  }

  // 设为在线
  Future<bool> setOnline({int state = UserState.online}) async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userSetOnline,
      method: XinDongHttpMethod.post,
      parameters: {"state": state},
    );
    return object.code == XinDongServerStatus.success;
  }

  // 推送
  Future<bool> resetPush() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userResetPush,
      method: XinDongHttpMethod.post,
    );
    return object.code == XinDongServerStatus.success;
  }

  // 版本
  Future<bool> setVersion() async {
    final object = await XinDongNetworking.instance.request(
      XinDongApi.userVersion,
      method: XinDongHttpMethod.post,
      parameters: {
        "version": XinDongAppDeviceInfo.instance.packageInfo?.version,
        "phone_name": XinDongAppDeviceInfo.instance.deviceName, // 手机品牌
      },
    );
    return object.code == XinDongServerStatus.success;
  }
}
